import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView/HomeView.vue'
import ConcernView from '../views/ConcernView/ConcernView.vue'
import FairView from '../views/FairView/FairView.vue'
import UserView from '../views/UserView/UserView.vue'
import ExifView from '../views/ExifView/ExifView.vue'

Vue.use(VueRouter)

const routes = [
  // 自动重定向
  {
    path: '',
    redirect: "/home"
  },
  {
    path: '/',
    redirect: '/home'  //默认显示
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [
      {
        path: 'search',
        name: 'search',
        component: () => import("../views/HomeView/HomeSearchView.vue")
      },
      {
        path: 'item',
        name: 'item',
        component: () => import("../views/HomeView/HomeItem.vue")
      },
      {
        path: 'granary-nav-item',
        name: 'granary-nav-item',
        component: () => import("../views/HomeView/GranaryNavItem.vue")
      }
    ]
  },
  {
    path: '/concern',
    name: 'concern',
    component: ConcernView,
  },
  {
    path: '/exif',
    name: 'exif',
    component: ExifView,
  },
  {
    path: '/fair',
    name: 'fair',
    component: FairView,
  },
  {
    path: '/user',
    name: 'user',
    component: UserView,
    children: [
      {
        path: 'my-love',
        name: 'my-love',
        component: () => import("../views/UserView/MyLove.vue")
      },
      {
        path: 'my-Collection',
        name: 'my-Collection',
        component: () => import("../views/UserView/MyCollection.vue")
      },
      {
        path: 'my-footsteps',
        name: 'my-footsteps',
        component: () => import("../views/UserView/MyFootsteps.vue")
      },
      {
        path: 'page',
        name: 'page',
        component: () => import("../views/UserView/HomePage.vue")
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import("../views/UserView/LoginView.vue")
  },
  {
    path: '/longin-details',
    name: 'longin-details',
    component: () => import("../views/UserView/LonginDetails.vue"),
  }
]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  let userInfo = sessionStorage.getItem('15196118927');		//获取登录存储的数据
  if (to.path === '/login' || to.path === '/home' || to.path === '/concern' || to.path === '/longin-details') {
    //如果当前进入页面是登录或注册就不执行任何操作，继续进入。
    next();
  } else {//否则判断是否存在登录存储的数据，不存在就进入登录页面
    if (userInfo === null) {
      next('/login');
    } else {
      next();
    }
  }
});




export default router
